function  [bdraw,m1,CC1]=drawncdiff(y,x,prm,prprec,sig,tole);
% function  [bdraw,m1,CC1]=drawncdiff(y,x,prm,prprec,sig,tole);
% Draw Coefficients from a Normal distribution when prior is also 
%      Normal( prm, prprec^(-1) )  and prprec is the precision
%      subject to rejection sampling with the constraints that the coefficients 
%      be orderder from smallest to largest 
%      i.e.  b(2) - b(1) > tole(1) 
%            b(3) - b(2) > tole(2) 
%            etc. 
%
% y      (Txn) 
% x      (TxK)  
% Prm    (Kx1)  Prior Mean 
% prprec (KxK)  Prior Precision
% sig    (1x1)  Variance of the errors
% tole   (1x K-1) vector of constraints for the difference of coefficients 
%
%
% bdraw  (1xK)  Draw from the posterior normal as a row vector 
% m1     (1xK)  Posterior mean 
% CC1    (KxK)  Cholelsky of the posterior variance 
%               V1= CC1'*CC1  
[ty,ny]=size(y); 
[tx,nx]=size(x); 
nk=length(prm);
if tx~=ty
    error('Not equal number of observations in drawcoef'); 
end; 

V1=( prprec+ ( 1/ sig)*(x'*x)  ) \ eye(nk) ;   % Posterior Variance  
m1=V1*(prprec*prm + (1/sig)*(x'*y) );        % Posterior Mean  
CC1=chol(V1); 

acc=0;  
rcoun=1; 
while acc <1
    bdraw = m1+ CC1*randn(nk,1) ;
    if all( [bdraw(2:end)' - bdraw(1:end-1)'] > tole ) ==1; 
        acc=1;  
    end 
    rcoun=rcoun+1; 
    if rem(rcoun,500) == 0; 
        warning('500 draws have not yet satisifed the constraint in drawNcdiff.m') 
    end 
end 
bdraw=bdraw'; 
m1=m1';
rcoun=rcoun-1; 